package com.book.lizhi.ui;

import java.util.List;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import cn.waps.AdInfo;
import cn.waps.AppConnect;

import com.android.volley.toolbox.NetworkImageView;
import com.book.lizhi.AppApplication;
import com.book.lizhi.R;
import com.book.lizhi.adapter.AppAdapter;

public class AdDetailActivity extends ActionBarActivity {
	private AdInfo mAdInfo;
	private OnClickListener mDownloadListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			AppConnect.getInstance(getApplicationContext()).downloadAd(getApplicationContext(), mAdInfo.getAdId());
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_ad_detail);
		Toolbar toolbar = (Toolbar) findViewById(R.id.toobar);
		setSupportActionBar(toolbar);
		ActionBar actionBar = getSupportActionBar();
		actionBar.setTitle("应用详情");
		actionBar.setDisplayHomeAsUpEnabled(true);

		mAdInfo = AppApplication.instance.adInfo;

		ImageView iconView = (ImageView) findViewById(R.id.adv_icon);
		TextView nameView = (TextView) findViewById(R.id.adv_name);
		TextView hdesView = (TextView) findViewById(R.id.adv_des);
		TextView textView = (TextView) findViewById(R.id.adv_text);
		iconView.setImageBitmap(mAdInfo.getAdIcon());
		nameView.setText(mAdInfo.getAdName());
		hdesView.setText(mAdInfo.getVersion());
		textView.setText(mAdInfo.getFilesize());

		TextView desView = (TextView) findViewById(R.id.textView1);
		desView.setText("简介:\n" + mAdInfo.getDescription());

		LinearLayout viewFlow = (LinearLayout) findViewById(R.id.image_container);
		for (String string : mAdInfo.getImageUrls()) {
			Log.e("", string);
			View view = View.inflate(getApplicationContext(), R.layout.ad_image_item, null);
			NetworkImageView imageView = (NetworkImageView) view.findViewById(R.id.image_networkimageview);
			imageView.setImageUrl(string, AppApplication.instance.imageLoader);
			viewFlow.addView(view);
		}

		findViewById(R.id.download_btn).setOnClickListener(mDownloadListener);
		findViewById(R.id.btn_appstore_button).setOnClickListener(mDownloadListener);

	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == android.R.id.home) {
			finish();
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	class ImageAdapter extends AppAdapter {

		public ImageAdapter(Context context, List<Object> list) {
			super(context, list);
		}

		@Override
		public View getView(int position, View convertView, Context context) {
			NetworkImageView imageView = new NetworkImageView(context);
			imageView.setAdjustViewBounds(true);
			imageView.setDefaultImageResId(R.drawable.splash);
			imageView.setImageUrl(getItem(position).toString(), AppApplication.instance.imageLoader);
			return imageView;
		}

	}
}
